/*
* $Id: FamilyManagerTest.cpp 156 2010-09-28 19:00:52Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "Components/Journal/FamilyManager.h"
#include "Sources/Database/QueryResult.h"
#include <cppunit/TestCase.h>
#include<cppunit/extensions/HelperMacros.h>

class CFamilyManagerTest : public CppUnit::TestCase
{
	CPPUNIT_TEST_SUITE(CFamilyManagerTest);
	CPPUNIT_TEST(InitTest);
	CPPUNIT_TEST(AddPointTest);
	CPPUNIT_TEST(AddFamilyTest);
	CPPUNIT_TEST(ChangePointTest);
	CPPUNIT_TEST(ChangeFamilyTest);
	CPPUNIT_TEST_SUITE_END();
private:
	std::vector<CTableItem> GetRowPoint(const std::tstring& strId,
		const std::tstring& strName, const std::tstring& strNote,
		const std::tstring& strIdRecord);
	std::vector<CTableItem> GetRowFamily(const std::tstring& strId,
		const std::tstring& strIdPoint, const std::tstring& strName,
		const std::tstring& strNote, const std::tstring& strType,
		const std::tstring& strIdRecord);
	void Init(CFamilyManager* pManager);
public: 
	CFamilyManagerTest(){};
	void InitTest();
	void AddPointTest();
	void AddFamilyTest();
	void ChangePointTest();
	void ChangeFamilyTest();
};

std::vector<CTableItem> CFamilyManagerTest::GetRowPoint( const std::tstring& strId, const std::tstring& strName,
													const std::tstring& strNote, const std::tstring& strIdRecord){
	std::vector<CTableItem> m_aItems;
	CTableItem item = {_T("id_point"), strId};
	m_aItems.push_back(item);
	CTableItem item1 = {_T("name"), strName};
	m_aItems.push_back(item1);
	CTableItem item2 = {_T("note"), strNote};
	m_aItems.push_back(item2);
	CTableItem item3 = {_T("id_record"), strIdRecord};
	m_aItems.push_back(item3);
	return m_aItems;
}

std::vector<CTableItem> CFamilyManagerTest::GetRowFamily( const std::tstring& strId, const std::tstring& strIdPoint,
		const std::tstring& strName, const std::tstring& strNote, const std::tstring& strType, const std::tstring& strIdRecord){
	std::vector<CTableItem> m_aItems;
	CTableItem item = {_T("id_family"), strId};
	m_aItems.push_back(item);
	CTableItem item1 = {_T("name"), strName};
	m_aItems.push_back(item1);
	CTableItem item2 = {_T("note"), strNote};
	m_aItems.push_back(item2);
	CTableItem item3 = {_T("id_record"), strIdRecord};
	m_aItems.push_back(item3);
	CTableItem item4 = {_T("id_point"), strIdPoint};
	m_aItems.push_back(item4);
	CTableItem item5 = {_T("type"), strType};
	m_aItems.push_back(item5);
	return m_aItems;
}

void CFamilyManagerTest::Init(CFamilyManager* pManager){
	g_QR.Init();
	MyTable aTable;
	aTable.push_back(GetRowPoint(_T("1"),_T("point1"),_T("descr"),_T("0")));
	aTable.push_back(GetRowPoint(_T("2"),_T("point2"),_T("descr"),_T("0")));
	g_QR.AddResult(aTable);
	aTable.clear();
	aTable.push_back(GetRowFamily(_T("1"), _T("1"), _T("1a"),_T("descr"),_T("1"), _T("0")));
	aTable.push_back(GetRowFamily(_T("2"), _T("1"), _T("2a"),_T("descr"),_T("2"), _T("0")));
	aTable.push_back(GetRowFamily(_T("3"), _T("2"), _T("3a"),_T("descr"),_T("2"), _T("0")));
	aTable.push_back(GetRowFamily(_T("4"), _T("2"), _T("4a"),_T("descr"),_T("4"), _T("0")));
	aTable.push_back(GetRowFamily(_T("5"), _T("1"), _T("5a"),_T("descr"),_T("5"), _T("0")));
	aTable.push_back(GetRowFamily(_T("6"), _T("1"), _T("6a"),_T("descr"),_T("4"), _T("0")));
	g_QR.AddResult(aTable);
	pManager->Init();
}

void CFamilyManagerTest::InitTest(){
	CFamilyManager manager;
	Init(&manager);

	std::map<int, CFamilyManager::point> aPoints = manager.GetPoints();
	CPPUNIT_ASSERT(aPoints.size() == 2);
	std::map<int, CFamilyManager::family> aFamilies = manager.GetFamilies();
	CPPUNIT_ASSERT(aFamilies.size() == 6);
	aFamilies = manager.GetFamilies(1);
	CPPUNIT_ASSERT(aFamilies.size() == 4);
	aFamilies = manager.GetFamilies(2);
	CPPUNIT_ASSERT(aFamilies.size() == 2);
}

void CFamilyManagerTest::AddPointTest(){
	CFamilyManager manager;
	Init(&manager);

	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_point"), _T("3")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);

	CFamilyManager::point pnt;
	pnt.m_strName = _T("point3");
	pnt.m_strNote = _T("descr");
	manager.AddPoint(pnt, std::map<std::tstring, std::tstring>());
	std::map<int, CFamilyManager::point> aPoints = manager.GetPoints();
	CPPUNIT_ASSERT(aPoints.size() == 3);
	CPPUNIT_ASSERT(aPoints[3].m_strName == _T("point3"));
}

void CFamilyManagerTest::AddFamilyTest(){
	CFamilyManager manager;
	Init(&manager);

	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_point"), _T("7")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);

	CFamilyManager::family fml;
	fml.m_strName = _T("7a");
	fml.m_strNote = _T("descr");
	fml.m_nIdPoint = 2;
	manager.AddFamily(fml, std::map<std::tstring, std::tstring>());
	std::map<int, CFamilyManager::family> aFamilies = manager.GetFamilies(2);
	CPPUNIT_ASSERT(aFamilies.size() == 3);
	CPPUNIT_ASSERT(aFamilies[7].m_strName == _T("7a"));
}

void CFamilyManagerTest::ChangePointTest(){
	CFamilyManager manager;
	Init(&manager);

	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_record"), _T("0")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);

	CFamilyManager::point pnt;
	pnt.m_strName = _T("point3");
	pnt.m_strNote = _T("descr");
	manager.ChangePoint(2, pnt, std::map<std::tstring, std::tstring>());
	CPPUNIT_ASSERT(manager.GetPoint(2).m_strName == _T("point3"));
}

void CFamilyManagerTest::ChangeFamilyTest(){
	CFamilyManager manager;
	Init(&manager);

	MyTable aTable;
	std::vector<CTableItem> aItems;
	CTableItem item = {_T("id_record"), _T("0")};
	aItems.push_back(item);
	aTable.push_back(aItems);
	g_QR.AddResult(aTable);

	CFamilyManager::family fml = manager.GetFamily(2);
	fml.m_strName = _T("3a");
	manager.ChangeFamily(2, fml, std::map<std::tstring, std::tstring>());
	CPPUNIT_ASSERT(manager.GetFamily(2).m_strName == manager.GetFamily(3).m_strName);
}

CPPUNIT_TEST_SUITE_REGISTRATION(CFamilyManagerTest);